﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;

namespace WebSys.UI.OfficeFlow
{
    public class WorkFlowResourceSkin : MPBasicTemplate
    {
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        public Image fjimg;
        //protected override void CreateChildControls()
        //{
        //    base.ThemeName = "OfficeFlow";
        //    base.CreateChildControls();
        //}
        protected override void OnLoad(EventArgs e)
        {
            LoginHelper.CheckLogin();
            InitControl();
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                Initcy();
            }
        }

        private void InitControl()
        {
            //if (!string.IsNullOrEmpty(base.Page.Request["extname"]))
            //{
            //    string strFileType = base.Page.Request["extname"].Replace(".", "").ToLower();
            //    if (strFileType.CompareTo("jpg") == 0 || strFileType.CompareTo("jpeg") == 0 ||
            //        strFileType.CompareTo("gif") == 0 || strFileType.CompareTo("png") == 0 ||
            //        strFileType.CompareTo("bmp") == 0 || strFileType.CompareTo("tif") == 0)
            //    {
            //        if (!string.IsNullOrEmpty(base.Page.Request["aid"]))
            //        {
            //            base.Page.Response.BufferOutput = true;
            //            base.Page.Response.Clear();
            //            base.Page.Response.ContentType = string.Format("image/{0}", strFileType);
            //            byte[] bArr = _FlowFactoryProvider.getAttByAID("0", base.Page.Request["aid"]);
            //            base.Page.Response.BinaryWrite(bArr);
            //            base.Page.Response.Flush();
            //        }
            //    }
            //    else
            //    {
            //        var url = "workflowworkitem.aspx?aid=" + base.Page.Request["aid"] + "&wiid=" + base.Page.Request["wiid"] + "&iid=" + base.Page.Request["iid"] + "&input_index=0&userid=" + base.Page.Session["userid"] + "&ctlid=" + base.Page.Request["ctlid"] + "&wid=" + base.Page.Request["wid"] + "&act=ViewAttachment";
            //        base.Page.Response.Redirect(url);
            //    }
            //}
            fjimg = (Image)FindControl("fjimg");
        }


        private void Initcy()
        {
            if (!string.IsNullOrEmpty(base.Page.Request["aid"]))
            {
                string strSql = "select  *  from  st_attachment where aid='" + base.Page.Request["aid"] + "'";
                DataTable ds = _FlowFactoryProvider.GetOneDs("(" + strSql + ")", "*", "").Tables[0];
                if (ds != null && ds.Rows.Count > 0 && ds.Rows[0]["DATA"] != null)
                {
                    base.Page.Response.BufferOutput = true;
                    base.Page.Response.Clear();
                    byte[] bArr = _FlowFactoryProvider.getAttByAID("0", base.Page.Request["aid"]);
                    string fileext = ds.Rows[0]["EXT_NAME"].ToString();
                    if (fileext.ToLower() == ".xls" || (fileext.ToLower() == ".doc") || (fileext.ToLower() == ".docx"))
                    {
                        fjimg.Visible = false;
                        if ((fileext.ToLower() == ".xls"))
                        {
                            base.Page.Response.ContentType = "application/vnd.ms-excel";//打开文件类型
                        }
                        else if ((fileext.ToLower() == ".doc") || (fileext.ToLower() == ".docx"))
                        {
                            base.Page.Response.ContentType = "application/msword";//打开文件类型
                        }
                        base.Page.Response.BinaryWrite(bArr); //输出到网页         
                        base.Page.Response.Flush();
                    }
                    else if (fileext.ToLower() == ".jpg" || fileext.ToLower() == ".jpeg" ||
                        fileext.ToLower() == ".gif" || fileext.ToLower() == ".bmp"
                        || fileext.ToLower() == ".png" || fileext.ToLower() == ".tif"
                        || fileext.ToLower() == ".tiff")
                    {
                        //string bs = System.Text.Encoding.Default.GetString(bArr);
                        fjimg.ImageUrl = "../../../../ZhengShou/OfficeFlow/ShowImage.ashx?aid=" + base.Page.Request["aid"];
                        fjimg.Attributes.Add("style", "zoom:100%");
                        fjimg.Visible = true;
                    }

                }
            }

        }


    }
}




